package com.dd.cloud.user.controller;

import com.dd.cloud.common.annotate.RedisNoCache;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.user.config.TencentImProperties;
import com.dd.cloud.user.entity.plat.PlatformWechatInfo;
import com.dd.cloud.user.req.plat.PlatLoginAccountReq;
import com.dd.cloud.user.res.plat.PlatLoginRes;
import com.dd.cloud.user.res.plat.ShareWechatInfo;
import com.dd.cloud.user.res.plat.TencentImUserSig;
import com.dd.cloud.user.service.plat.IPlatformLoginService;
import com.dd.cloud.user.service.plat.IPlatformWechatInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.util.stream.Collectors;

/**
 * <p>
 * 第三方登录信息表 前端控制器
 * </p>
 *
 * @author czg
 * @since 2022-05-06
 */
@Slf4j
@RestController
@RequestMapping("/plat")
public class PlatformLoginController {
    @Autowired
    private IPlatformLoginService platformLoginService;
    @Autowired
    private IPlatformWechatInfoService platformWechatInfoService;

    /**
     * 微信分享返回的数据
     *
     * @param shareUrl
     * @return
     */
    @GetMapping(value = "/wechat/share")
    public ResponseEntity<ShareWechatInfo> shareWechatInfo(@NotBlank(message = "分享的链接不能为空") String shareUrl) {
        ShareWechatInfo entity = platformWechatInfoService.shareWechatInfo(shareUrl);
        return ResponseEntity.ok(entity);
    }

    /**
     * 第三方登录
     *
     * @param req code
     * @return
     */
    @RedisNoCache
    @PostMapping("/login/plat")
    public ResponseEntity<PlatLoginRes<?>> loginPlat(HttpServletRequest request, HttpServletResponse response, PlatLoginAccountReq req, BindingResult result) {
        if (result.hasErrors()) {
            String errors = result.getFieldErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining("|"));
            throw new ZekeException(HttpStatus.BAD_REQUEST.value(), errors);
        }
        log.info("第三方登录信息={}", req);
        PlatLoginRes<?> res = platformLoginService.platLogin(request, response, req);
        return ResponseEntity.ok(res);
    }

    /**
     * 公众号获取openId
     *
     * @return
     */
    @GetMapping("/wechat/openId")
    public ResponseEntity<PlatformWechatInfo> wechatOpenId(String code, HttpServletResponse response, String appId) {
        PlatformWechatInfo info = platformLoginService.getWechatOpenid(response, code, appId);
        return ResponseEntity.ok(info);
    }

    /**
     * 解绑微信账号
     */
    @PostMapping("/unbind/wx")
    public ResponseEntity<Void> unbindAccountWx() {
        platformLoginService.unbindAccountWx();
        return ResponseEntity.ok().build();
    }

    /**
     * 获取医生端小程序accesstoken
     *
     * @return String
     */
    @GetMapping("/wechat/applet/access/token/doctor")
    public ResponseEntity<String> getDoctorAccessToken() {
        String res = platformLoginService.getDoctorAccessToken();
        return ResponseEntity.ok(res);
    }

    /**
     * 小程序获取openId
     *
     * @return
     */
    @GetMapping("/wechat/applet/config")
    public ResponseEntity<String> getAppletConfig(@NotBlank(message = "小程序code不能为空") String code) {
        String info = platformLoginService.getAppletConfig(code);
        return ResponseEntity.ok(info);
    }


//    /**
//     * 小程序获取openId
//     *
//     * @return
//     */
//    @GetMapping("/wechat/user/applet/config")
//    public ResponseEntity<String> getUserAppletConfig(@NotBlank(message = "小程序code不能为空") String code) {
//        String info = platformLoginService.getUserAppletConfig(code);
//        return ResponseEntity.ok(info);
//    }


    /**
     * 返回腾讯云im即时通信的参数
     *
     * @return
     */
    @GetMapping("/tencent/im/secret")
    public ResponseEntity<TencentImProperties> tencentImSecret() {
        TencentImProperties info = platformWechatInfoService.tencentImSecret();
        return ResponseEntity.ok(info);
    }

    /**
     * 返回腾讯云im即时通信的用户签名
     *
     * @return
     */
    @GetMapping("/tencent/im/userSig")
    public ResponseEntity<TencentImUserSig> tencentImUserSig(Integer id, Integer type, String medId) {
        TencentImUserSig info = platformWechatInfoService.tencentImUserSig(id, type, medId);
        return ResponseEntity.ok(info);
    }


//    /**
//     * 分页查询app版本信息
//     *
//     * @return
//     */
//    @GetMapping("/getui/test")
//    public ResponseEntity<Void> getuiTest(String cid, AppNewsSendReq req) {
//        geTuiUtils.pushAll(req);
////        sensorPush.pushAll(req);
//        return ResponseEntity.ok().build();
//    }

    /**
     * 微信回调验证
     *
     * @return
     */
    @PostMapping("/wx/verify")
    public String verifyWXToken(HttpServletRequest request) {
//        String msgSignature = request.getParameter("signature");
//        String msgTimestamp = request.getParameter("timestamp");
//        String msgNonce = request.getParameter("nonce");
//        String echostr = request.getParameter("echostr");
////        //if (WXPublicUtils.verifyUrl(msgSignature, msgTimestamp, msgNonce)) {
//            return echostr;
//        //}
        log.info("扫码回调");
        return platformWechatInfoService.handlePublicMsg(request);

    }

    /**
     * 自定义发布公众号菜单
     *
     * @return
     */
    @PostMapping("/wx/public/menu")
    public String wxPublicMenu(HttpServletRequest request, String menuJson) {
        log.info("发布菜单,{}", menuJson);
        return platformWechatInfoService.wxPublicMenu(request, menuJson);

    }
}

